package com.fei.controller;

import com.fei.bean.AdminBean;
import com.fei.bean.SongListBean;
import com.fei.mapper.SongListMapper;
import com.fei.service.FileService;
import com.fei.service.SongListMusicService;
import com.fei.service.SongListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

@Controller
public class SongListController {

    @Autowired
    SongListService songListService;
    @Autowired
    FileService fileService;
    @Autowired
    SongListMusicService songListMusicService;

    @RequestMapping("/admin/toSongList")
    public String toSongList(Model model){
        List<SongListBean> songListBean = songListService.findAllSongListService();
        model.addAttribute("songListBean",songListBean);
        return "song_list";
    }




//    添加歌单
    @RequestMapping("/admin/add/songListInsert")
    public String songListInsert(SongListBean songListBean, HttpSession session, MultipartFile file){
        AdminBean admin = (AdminBean)session.getAttribute("admin");
        songListBean.setProvider(admin.getName());

        File parentPath = fileService.ParentPath();
        String Path = parentPath + "//data//Song List//" + songListBean.getTitle();
        String fileSuffix = fileService.FileSuffix(file.getOriginalFilename());
        fileService.FileUpload(Path,"1."+fileSuffix,file);

        songListBean.setImage("/Song List/"+songListBean.getTitle()+"/1."+fileSuffix);

        songListService.insertSongListService(songListBean);
        return "redirect:/admin/toSongList";
    }

//    删除歌单
    @RequestMapping("/admin/delete/deleteSongList")
    public String deleteSongList(int id){

        SongListBean songListBean = songListService.selectOneIdSongListService(id);
        //删除对应的文件
        File parentPath = fileService.ParentPath();
        fileService.FileDelete(new File(parentPath + "/data/Song List/" + songListBean.getTitle()));

        //删除对应的歌单歌曲字段
        songListMusicService.deleteAllOneSongListIdService(id);

        songListService.deleteSongListService(id);
        return "redirect:/admin/toSongList";
    }


}
